-- TL_RaidTracker by Amandria of Tempest Legends (Blackmoore EU)
-- $Id: Core.lua 11 2008-01-14 21:23:44Z maaki@gmx.de $
-- http://code.google.com/p/tl-wow/

local options = {
	type = 'group',
	args = {
		about = {
			type = 'execute',
			name = 'about',
			desc = 'Show information on this Addon',
			func = 'About',
		},
		start = {
			type = 'execute',
			name = 'start',
			desc = 'Start tracking the raid',
			func = 'TrackStart',
		},
		stop = {
			type = 'execute',
			name = 'stop',
			desc = 'Stop tracking the raid',
			func = 'TrackStop',
		},
		backup = {
			type = 'text',
			name = 'backup',
			desc = 'Mark a player as backup',
			usage = '<player>',
			get  = false, 
			set  = 'PutBackup',
		},
	},
};

local trackingActive = false;
local cRaid = nil;
local lastBoss = "Trash";

local ItemSingle = "(|c%x+|Hitem:%d+:%d+:%d+:%d+:%d+:%d+:(%-?%d+):(%-?%d+)|h%[.-%]|h|r)%";
local ItemMulti  = "(|c%x+|Hitem:%d+:%d+:%d+:%d+:%d+:%d+:(%-?%d+):(%-?%d+)|h%[.-%]|h|r)x(%d+)%";
local ItemOtherSingle = "([^%s+]) receives loot: " .. ItemSingle .. ".";
local ItemSelfSingle = "You receive loot: " .. ItemSingle .. ".";
local ItemOtherMulti = "([^%s+]) receives loot: " .. ItemMulti .. ".";
local ItemSelfMulti = "You receive loot: " .. ItemMulti .. ".";

RaidTracker = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0", "AceDB-2.0", "AceEvent-2.0", "FuBarPlugin-2.0")
RaidTracker:RegisterChatCommand("/raidtracker", "/rt", options);
RaidTracker:RegisterDB("RaidTrackerDB");
bb = AceLibrary("Babble-Boss-2.2");
deformat = AceLibrary("Deformat-2.0");

function RaidTracker:OnInitialize()
	self.OnMenuRequest = options;
	if ( not self.db.account.raids ) then
		self.db.account.raids = {};
	end
end

function RaidTracker:OnEnable()
	self:Print(" v." .. self.version .. " loaded.");
end

function RaidTracker:OnDisable()
	self:Print(" v." .. self.version .. " unloaded.");
end

function RaidTracker:OnTextUpdate()
	self:SetText("RT");
end


function RaidTracker:CHAT_MSG_COMBAT_HOSTILE_DEATH(arg1)
	local mob = deformat(arg1, UNITDIESOTHER)
	if ( mob ) then
		if bb:HasReverseTranslation(mob) then
			self:AddInfo({"kill", mob, date("%H:%M:%S")} );
			lastBoss = mob;
		end
	end
end

function RaidTracker:CHAT_MSG_LOOT(arg1)
	if (string.find(arg1, ItemOtherSingle)) then
		count = 1;
		_, _, player, item = string.find(arg1, ItemOtherSingle);
	elseif (string.find(arg1, ItemSelfSingle)) then
		count = 1;
		player = UnitName("player");
		_, _, item = string.find(arg1, ItemSelfSingle);
	elseif (string.find(arg1, ItemOtherMulti)) then
		_, _, player, item, count = string.find(arg1, ItemOtherMulti);
	elseif (string.find(arg1, ItemSelfMulti)) then
		player = UnitName("player");
		_, _, item, count = string.find(arg1, ItemSelfMulti);
	end
	self:AddLoot( {player, item, count, lastBoss, date("%H:%M:%S")} );
end

function RaidTracker:CHAT_MSG_SYSTEM(arg1)
	local _, _, player = string.find(arg1, "([^%s]+) has joined the raid group.");
	if ( player and player ~= UnitName("player") ) then
		self:AddEvent( {"join", player, date("%H:%M:%S")} );
	end
	local _, _, player = string.find(arg1, "([^%s]+) has left the raid group.");
	if ( player and player ~= UnitName("player") ) then
		self:AddEvent( {"leave", player, date("%H:%M:%S")} );
	end
end


function RaidTracker:About()
	self:Print(" v." .. self.version .. " by " ..  self.author .. ".");
end

function RaidTracker:TrackStart()
	if ( not trackingActive ) then
		trackingActive = true;
		cRaid = {
			rid = date("%Y%m%d - %H:%M:%S");
			info = {},
			loot = {},
			event = {},
		};
		self:AddInfo( {"start", date("%H:%M:%S") } );
		self:Print("tracking started.");
		for i = 1, GetNumRaidMembers() do
			self:AddEvent( { "join", UnitName("raid" .. i), date("%H:%M:%S") } );
		end
		self:RegisterEvent("CHAT_MSG_COMBAT_HOSTILE_DEATH");
		self:RegisterEvent("CHAT_MSG_LOOT");
		self:RegisterEvent("CHAT_MSG_SYSTEM");
	else
		self:Print("Error: cannot start tracking - tracking already started.")
	end
end

function RaidTracker:TrackStop()
	if ( trackingActive ) then
		trackingActive = false;
		self:UnregisterEvent("CHAT_MSG_COMBAT_HOSTILE_DEATH");
		self:UnregisterEvent("CHAT_MSG_LOOT");
		self:UnregisterEvent("CHAT_MSG_SYSTEM");
		self:AddInfo( {"stop", date("%H:%M:%S") } );
		self:Print("tracking stopped.");
		self.db.account.raids[cRaid['rid']] = cRaid;
		self.db.account.lastRaid = {};
		cRaid = nil;
		lastBoss = "Trash";
	else
		self:Print("Error: cannot stop tracking - tracking already stopped.");
	end
end

function RaidTracker:PutBackup(player)
	if ( trackingActive ) then
		self:AddEvent( {"backup", player} );
		self:Print("Marked " .. player .. " as backup.");
	else
		self:Print("Error: cannot mark " .. player .. " as backup - tracking is stopped.");
	end
end


function RaidTracker:AddEvent(data)
	table.insert(cRaid['event'], data);
	self.db.account.lastRaid = cRaid;
end

function RaidTracker:AddInfo(data)
	table.insert(cRaid['info'], data);
	self.db.account.lastRaid = cRaid;
end

function RaidTracker:AddLoot(data)
	table.insert(cRaid['loot'], data);
	self.db.account.lastRaid = cRaid;
end
